package com.moore.Servlet;

import com.alibaba.fastjson2.JSON;
import com.moore.net.HttpRequest;
import com.moore.net.HttpResponse;
import com.moore.net.HttpServlet;
import com.moore.utils.GoodsUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

public class GoodsServlet extends HttpServlet {
    @Override
    public byte[] doGet(HttpRequest request, HttpResponse response) {
        String responseData = "";
        String num = request.getParams().get("num");
        switch (num) {
            case "1":
                Random random = new Random();
                Set<Integer> numberSet = new HashSet<>();
                while (numberSet.size() < 5) {
                    numberSet.add(random.nextInt(15) + 1);
                }
                String[] number = numberSet.stream().map(String::valueOf).toArray(String[]::new);
                responseData = JSON.toJSONString(GoodsUtils.getGoods(null,null, null, number));
                break;
            case "2": {
                String id = request.getParams().get("id");
                responseData = JSON.toJSONString(GoodsUtils.getGoods(null, null,null, id));
                break;
            }
            case "3": {
                String id = request.getParams().get("id");
                responseData = GoodsUtils.getGoodsImage(id);
                break;
            }
            case "4":
                String type = request.getParams().get("type");
                responseData = JSON.toJSONString(GoodsUtils.getGoods("0",null, type, "0"));
                break;
            case "5": {
                String time;
                try {
                    time = URLDecoder.decode(request.getParams().get("time"), "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    throw new RuntimeException(e);
                }
                List<String> list = GoodsUtils.getSecKillGoods("goods_id",time);
                String[] array = list.toArray(new String[0]);
                responseData = JSON.toJSONString(GoodsUtils.getGoods(null,null, null, array));
                break;
            }
            case "6": {
                String time;
                try {
                    time = URLDecoder.decode(request.getParams().get("time"), "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    throw new RuntimeException(e);
                }
                List<String> list = GoodsUtils.getSecKillGoods("price",time);
                responseData = JSON.toJSONString(list);
                break;
            }
            case "7": {
                String id = request.getParams().get("id");
                responseData = JSON.toJSONString(GoodsUtils.getSecKillGoods(id));
                break;
            }
            case "8":
                String keyword;
                try {
                    keyword = URLDecoder.decode(request.getParams().get("keyword"), "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    throw new RuntimeException(e);
                }
                responseData = JSON.toJSONString(GoodsUtils.getGoods(null, keyword, null, "0"));
                break;
            case "9":
                responseData = JSON.toJSONString(GoodsUtils.getSecKillGoods(null));
                break;
            case "10": {
                String id = request.getParams().get("id");
                responseData = JSON.toJSONString(GoodsUtils.getNames(id));
                break;
            }
        }
        return responseData.getBytes(StandardCharsets.UTF_8);
    }
}
